package cn.stylefeng.guns.modular.feature.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.feature.entity.Feature;
import cn.stylefeng.guns.modular.feature.mapper.FeatureMapper;
import cn.stylefeng.guns.modular.feature.model.params.FeatureParam;
import cn.stylefeng.guns.modular.feature.model.result.FeatureResult;
import cn.stylefeng.guns.modular.feature.service.FeatureService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 功能表 服务实现类
 * </p>
 *
 * @author jalor
 * @since 2019-10-20
 */
@Service
public class FeatureServiceImpl extends ServiceImpl<FeatureMapper, Feature> implements FeatureService {

    @Override
    public void add(FeatureParam param) {
        Feature entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FeatureParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(FeatureParam param) {
        Feature oldEntity = getOldEntity(param);
        Feature newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FeatureResult findBySpec(FeatureParam param) {
        return null;
    }

    @Override
    public List<FeatureResult> findListBySpec(FeatureParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FeatureParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 获取所有功能集合
     *
     * @param productId 产品ID
     * @param code      编号
     * @param name      名称
     * @return 功能集合
     */
    @Override
    public List<Feature> listAll(String productId, String code, String name) {
        return this.baseMapper.listAll(productId, code, name);
    }

    private Serializable getKey(FeatureParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Feature getOldEntity(FeatureParam param) {
        return this.getById(getKey(param));
    }

    private Feature getEntity(FeatureParam param) {
        Feature entity = new Feature();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
